<template>
  <div>
    <div slot="com_header"><bread></bread></div>
    <div slot="com_con">
      <!-- 订单修改 -->
      <el-dialog title="收货地址" :visible.sync="dialogUpd">
        <el-form :model="formUpd">
          <el-form-item label="订单id" label-width="180px">
            <el-input v-model="formUpd.order_id" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="是否送达" label-width="180px">
            <el-input v-model="formUpd.is_send" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="order_pay" label-width="180px">
            <el-input v-model="formUpd.order_pay" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="order_price" label-width="180px">
            <el-input
              v-model="formUpd.order_price"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="order_number" label-width="180px">
            <el-input
              v-model="formUpd.order_number"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="pay_status" label-width="180px">
            <el-input
              v-model="formUpd.pay_status"
              autocomplete="off"
            ></el-input>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogUpd = false">取 消</el-button>
          <el-button type="primary" @click="save">确 定</el-button>
        </div>
      </el-dialog>

      <!-- 订单详情 -->
      <el-dialog title="收货地址" :visible.sync="dialogId">
        <el-form :model="formId">
          <el-form-item label="订单Id" label-width="120px">
            <el-input
              v-model="formId.order_id"
              autocomplete="off"
              disabled
            ></el-input>
          </el-form-item>
          <el-form-item label="用户Id" label-width="120px">
            <el-input
              v-model="formId.user_id"
              autocomplete="off"
              disabled
            ></el-input>
          </el-form-item>
          <el-form-item label="订单价格" label-width="120px">
            <el-input
              v-model="formId.order_price"
              autocomplete="off"
              disabled
            ></el-input>
          </el-form-item>
          <el-form-item label="是否送达" label-width="120px">
            <el-input
              v-model="formId.is_send"
              autocomplete="off"
              disabled
            ></el-input>
          </el-form-item>
        </el-form>
      </el-dialog>
      <!-- 条件查询 -->
      <el-form :inline="false" :model="form" class="demo-form-inline">
        <el-row>
          <el-col :md="16">
            <el-form-item>
              <el-input v-model="form.query" placeholder="订单编号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :md="2">
            <el-form-item>
              <el-button type="normal" @click="query">查询</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- 列表查询 -->
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="order_id" label="序号" width="180">
        </el-table-column>
        <el-table-column prop="order_number" label="订单编号" width="180">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格"> </el-table-column>
        <el-table-column label="是否付款">
          <template slot-scope="scope">
            <el-tag type="danger" v-show="scope.row.order_pay == 0"
              >未付款</el-tag
            >
            <el-tag v-show="scope.row.order_pay == 1">支付宝支付</el-tag>
            <el-tag type="success" v-show="scope.row.order_pay == 2"
              >微信支付</el-tag
            >
            <el-tag type="warning" v-show="scope.row.order_pay == 3"
              >银行卡支付</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"> </el-table-column>
        <el-table-column prop="" label="下单时间">
          <template slot-scope="{ row }">
            <div v-html="$options.filters.fmdtime(row.create_time)"></div>
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="{ row }">
            <el-button type="warning" size="mini" @click="open(row)"
              >编辑</el-button
            >
            <el-button type="primary" @click="look(row)" size="mini"
              >查看详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="form.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="form.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import bread from "../../components/bread";
export default {
  components: {
    bread,
  },
  filters: {
    fmdtime: function (value) {
      const newdate = new Date(value);
      let str =
        newdate.getFullYear() +
        "-" +
        (newdate.getMonth() + 1) +
        "-" +
        newdate.getDate() +
        "<br/>" +
        newdate.getHours() +
        ":" +
        newdate.getMinutes() +
        ":" +
        newdate.getSeconds();
      return str;
    },
  },
  data() {
    return {
      tableData: [],
      form: {
        query: "",
        pagenum: 1,
        pagesize: 5,
        user_id: "",
        pay_status: "",
        is_send: "",
        order_fapiao_title: "",
        order_fapiao_company: "",
        order_fapiao_content: "",
        consignee_addr: "",
      },
      total: 0,

      // 订单详情
      formId: {},
      dialogId: false,

      //修改订单
      formUpd: {
        id: "",
        is_send: "",
        order_pay: "",
        order_price: "",
        order_number: "",
        pay_status: "",
      },
      dialogUpd: false,
    };
  },
  mounted() {
    this.query();
  },
  methods: {
    async query() {
      let rst = await this.$http.get("orders", this.form);
      let { data, meta } = rst;
      this.tableData = data.goods;
      this.total = data.total;
      console.log(rst);
    },

    // 分页
    handleSizeChange(val) {
      this.form.pagesize = val;
      this.query();
    },
    handleCurrentChange(val) {
      this.form.pagenum = val;
      this.query();
    },

    //  查看详情
    async look(row) {
      this.dialogId = true;
      let rst = await this.$http.get(`orders/${row.order_id}`);
      this.formId = rst.data;
      console.log(rst);
    },

    // 修改
    async open(row) {
      this.dialogUpd = true;
      console.log(row.order_id);
      let rst = await this.$http.get(`orders/${row.order_id}`);
      let { order_id, is_send, order_pay, order_price, order_number, pay_status } =
        rst.data;
      this.formUpd = {
        order_id,
        is_send,
        order_pay,
        order_price,
        order_number,
        pay_status,
      };
    },
    async save() {
      let rst = await this.$http.put(`orders/${this.formUpd.order_id}`, {
        is_send: this.formUpd.is_send,
        order_pay: this.formUpd.order_pay,
        order_price: this.formUpd.order_price,
        order_number: this.formUpd.order_number,
        pay_status: this.formUpd.pay_status,
      });
      console.log(rst);
      this.dialogUpd=false;
      this.query()
    },
  },
};
</script>

<style lang="less" scoped>
</style>